#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3 + 10;
int p[N];
int n, m;
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		memset(p, 0, sizeof(p));
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			p[i] = i;
		}
		while (m--) {
			int a, b;
			cin >> a >> b;
			if (find(a) != find(b)) {
				p[a] = b;
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (p[i] == i) {
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}